angular.module('changeorder', []).controller('changeorderCtrl', [
    '$scope',
    'httpService',
    '$stateParams',
    'alertmsg',
    '$timeout',
    'cities',
    '$rootScope',
    function($scope, httpService, $stateParams, alertmsg, $timeout, cities, $rootScope) {
        $scope.cities = cities.get();
        // 可以修改金额
        // $scope.amount_roles = ['manager', 'finance', 'edit_amount'];
        // var handleLists = ['manager', 'tech', 'business', 'finance', 'cashier', 'edit_order', 'edit_amount'];
        var id = $stateParams.id;
        $scope.isreview = $stateParams.isreview;
        var priceparams = {};
        var i = 1;
        var obj1 = {
            title: '',
            price: null,
            remark: 1
        };

        $scope.incidental = { 1: obj1 };
        $scope.arrs = [ obj1 ];

        $scope.getOrderById = function() {
            httpService.query('web/orders/' + id).then(function(res) {
                if (res.code == 200) {
                    $scope.order = res.order;
                    $scope.datas = angular.copy(res.order);
                    formatCity($scope.datas);
                    var len = $scope.order.incidentals.length;
                    if(len > 0) {
                        angular.forEach($scope.order.incidentals, function(v, k) {
                            delete v.id;
                            console.log('v', v)
                            v.remark = parseInt(v.remark);
                            v.price = parseFloat(v.price)/ 100
                            if( v.remark > i) {
                                i = v.remark;
                            }
                            $scope.incidental[v.remark] = v;
                        });
                        $scope.arrs = $scope.order.incidentals;
                        // i = len;
                    }
                    $timeout(function() {
                        $scope.order.total = parseFloat($scope.order.total);
                        $scope.order.sale_amount = parseFloat($scope.order.sale_amount / 100);
                        $scope.order.sale_commission = parseFloat($scope.order.sale_commission / 100);
                        $scope.order.intro_commission = parseFloat($scope.order.intro_commission / 100);
                    });
                    console.log('$scope.order.total', $scope.order)
                    // $scope.order.measure_amount = parseFloat($scope.order.measure_amount) / 100;
                    priceparams.material = res.order.material;
                    priceparams.square = res.order.square;
                }
            });
        };
        $scope.getOrderById();

        function formatCity(data) {
            angular.forEach($scope.cities, function(v, k) {
                if(v.name == data.province) {
                    $scope.datas.province = v;
                    angular.forEach(v.city, function(a, b) {
                        if(a.name == data.city) {
                            $scope.datas.city = a;
                        }
                    });
                }
            });
        }

        var first = 1;
        $scope.getPrice = function(square) {
            priceparams.square = square;
            if (square || square == 0) {
                httpService.query('web/orders/cal_price', priceparams, 'post').then(function(res) {
                    if (res.code == 200) {
                        if(first != 1) {
                            $scope.order.total = parseFloat(res.price);
                        }
                        ++ first;
                    }
                });
                
            } else {
                alertmsg('请输入窗体的面积!');
            }
        };
        // $scope.getPrice();

        // 可以修改面积
        $scope.$watch('order.square', function(newData, oldData) {
            if ((newData || newData == 0) && (role != 0 || $rootScope.handleLists.all.indexOf(user.user_type) != -1 )) {
                $scope.getPrice(newData);
            }
        });

        $scope.add = function() {
            ++i;
            var obj = {
                title: '',
                price: null,
                remark: i
            };
            $scope.arrs.push(obj);
            $scope.incidental[i] = obj;
        };

        $scope.delete = function(key) {
            $scope.arrs.splice(key, 1);
            var a = key + 1;
            delete $scope.incidental[a];
        };

        // 修改订单
        $scope.putOrder = function(changeorder) {
            var arrs = [];
            // for (var v in $scope.incidental) {
            //     console.log('v', $scope.incidental[v])
            //     arrs.push($scope.incidental[v]);
            // }
            var incidentals = angular.copy($scope.incidental);
            angular.forEach(incidentals, function(v, k) {
                v.price = parseFloat(v.price) * 100;
                arrs.push(v);
            });
            // create_incidental(arrs);
            // $scope.order.incidentals_attributes = arrs;
            var order = {
                incidentals_attributes: arrs,
                square: $scope.order.square,
                total: parseFloat($scope.order.total),
                measure_amount: $scope.order.measure_amount,
                street: $scope.order.street,
                sale_amount: parseFloat($scope.order.sale_amount) * 100,
                sale_commission: $scope.order.sale_commission * 100,
                intro_commission: $scope.order.intro_commission * 100
            };
            // console.log('order', order, arrs)
            var isSelectCity = false;
            $scope.firstUpdate = false;
            if($scope.datas && $scope.datas.province) { order.province = $scope.datas.province.name.trim(); } else { isSelectCity = true; }
            if($scope.datas && $scope.datas.city) { order.city = $scope.datas.city.name.trim(); } else { isSelectCity = true; }
            if($scope.datas && $scope.datas.area) { order.area = $scope.datas.area.trim(); } else { isSelectCity = true; }
            if(!isSelectCity && !$scope.firstUpdate) {
                $scope.firstUpdate = true;
                httpService.query('web/orders/' + id, order, 'put').then(function(res) {
                    if (res.code == 200) {
                        window.history.go(-1);
                    } else {
                        $scope.firstUpdate = false;
                        alertmsg(res.msg);
                    }
                }, function() { $scope.firstUpdate = false;});
            } else {
                if(isSelectCity) {alertmsg('请选择地址')}
                if($scope.firstUpdate) {alertmsg('正在上传！')}
            }
        };
    }
]);
